<template>
  <div>
   <home-header></home-header>
   <home-swiper :list="swiperList"></home-swiper>
   <home-icons :list01="iconList01" :list02="iconList02"></home-icons>
   <home-recommend :list01="htsList" :list02="recList"></home-recommend>
   <home-weekend :list="weekendList"></home-weekend>
  </div>
</template>

<script>
import HomeHeader from './components/Header'
import HomeSwiper from './components/Swiper'
import HomeIcons from './components/Icons'
import HomeRecommend from './components/Recommend'
import HomeWeekend from './components/Weekend'
import axios from 'axios'
import { mapState } from 'vuex'
export default{
  name: 'Home',
  components: {
    HomeHeader,
    HomeSwiper,
    HomeIcons,
    HomeRecommend,
    HomeWeekend
  },
  data: function(){
  	return {
  		lastCity: '',
  		swiperList: [],
  		iconList01: [],
  		iconList02: [],
  		htsList: [],
  		recList: [],
  		weekendList: []
  	}
  },
  computed: {
  	...mapState(['city'])
  },
  methods: {
  	//定义 getHomeInfo() 方法，这个方法就是帮我们获取 Ajax 数据
  	getHomeInfo(){
  		axios.get('/api/index.json?city=' + this.city)
  				 .then(this.getHomeInfoSucc)
  	},
  	getHomeInfoSucc(res){
  		res = res.data
  		if(res.ret && res.data){
  			const data = res.data
  			this.swiperList = data.swiperList
  			this.iconList01 = data.iconList01
  			this.iconList02 = data.iconList02
  			this.htsList = data.htsList
  			this.recList = data.recList
  			this.weekendList = data.weekendList
  		}
  		console.log(res)
  	}
  },
  mounted: function(){
  	this.lastCity = this.city
  	//让页面挂载好之后，执行 getHomeInfo() 函数
  	this.getHomeInfo()
  },
  activated () {
  	//当上一次城市与这次城市不同的时候
  	//让 this.lastCity 的值等于 this.city
  	//然后重新发一次 Ajax 请求
  	if (this.lastCity !== this.city) {
  		this.lastCity = this.city
  		this.getHomeInfo()
  	}
  }
}
</script>

<style lang="stylus" scoped>
	div
		background-color: #f5f5f5
</style>
